import axios from 'axios';
import router from '../router';
import { Message } from 'element-ui';
import qs from 'qs';
import Vue from 'vue';

axios.defaults.baseURL = process.env.VUE_APP_BASE_API;
console.log('baseURL', process.env.VUE_APP_BASE_API);
const goLogin = () => {
  if (router.currentRoute.name !== 'login') {
    router.replace({
      name: 'login',
    });
  } else {
    return;
  }
};

const instance = axios.create({});

instance.interceptors.request.use(
  (config) => {
    // config.headers = {
    //   'Content-Type': 'charset=UTF-8',
    // }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
instance.interceptors.response.use(
  (response) => {
    return response;
  },
  (err) => {
    const duration = 3 * 1000;
    const { response } = err;
    Message({
      message: err.message,
      type: 'error',
      duration,
    });

    setTimeout(() => {
      if (process.env.NODE_ENV !== 'development') {
        if (response && (response.status === 590 || response.status === 403)) {
          goLogin();
        }
      }
    }, duration);
    return Promise.reject(err);
  }
);
router.beforeEach((to, from, next) => {
  next();
});
router.afterEach((route) => {});

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  var aa = localStorage.getItem('token');
  let headers = {
    'Content-Type': 'application/json',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'get',
      url: url,
      headers,
      params: params,
    })
      .then((response) => {
        resolve(response.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
  //把拿到的taken存到localStorage,然后取出
  let headers = {
    'Content-Type': 'application/json',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装excel请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function excel(url, data = {}) {
  //把拿到的taken存到localStorage,然后取出
  let headers = {
    'Content-Type': 'application/json',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: url,
      headers,
      data: data,
      responseType: 'arraybuffer',
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  //把拿到的taken存到localStorage,然后取出
  let headers = {
    'Content-Type': 'application/json',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'put',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}
/**
 * 封装putone请求
 *
 *
 * @param url
 * @param data
 * @returns {Promise}
 */
export function putone(url, data = {}) {
  //把拿到的taken存到localStorage,然后取出
  let headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'put',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}
/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
  //把拿到的taken存到localStorage,然后取出
  let headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'patch',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

// 封装  patchJson
export function patchJson(url, data = {}) {
  //把拿到的taken存到localStorage,然后取出
  let headers = {
    'Content-Type': 'application/json',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'patch',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}
/**
 * 封装postJosn请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function postJosn(url, data = {}) {
  let headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装dalete请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function dalete(url, data = {}) {
  let headers = {
    'Content-Type': 'aapplication/x-www-form-urlencoded; charset=UTF-8',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'delete',
      url: url,
      headers,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

export function daleteJson(url, data = {}) {
  let headers = {
    'Content-Type': 'application/json; charset=UTF-8',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'delete',
      url: url,
      headers,
      traditional: true,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

export function download(url, data = {}) {
  let headers = {
    'Content-Type': 'application/json',
  };
  if (localStorage.getItem('token')) {
    headers = {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
    };
  }
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: url,
      headers,
      responseType: 'blob',
      traditional: true,
      data: data,
    }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}
